package dgut.haitaobao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import dgut.haitaobao.entry.IndexImg;
import dgut.haitaobao.entry.ShoppingCart;
import dgut.haitaobao.entry.ShoppingCartVO;
import dgut.haitaobao.vo.param.ShoppingCartParamsVO;
import dgut.haitaobao.mapper.ShoppingCartMapper;
import dgut.haitaobao.service.ShoppingCartService;
import dgut.haitaobao.util.response.HttpResponse;
import dgut.haitaobao.vo.param.ShoppingCartUpdateCartNumParamVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public HttpResponse getShoppingCarts(String userId) {
        List<ShoppingCartVO> shoppingCarts = shoppingCartMapper.getShoppingCartsByUserId(userId);
        /*if (!CollectionUtils.isEmpty(shoppingCarts)){
            return HttpResponse.success(shoppingCarts);
        }else {
            return
        }*/
        System.out.println(shoppingCarts);
        return HttpResponse.success(shoppingCarts);
    }

    @Override
    public HttpResponse addShoppingCar(ShoppingCartParamsVO shoppingCartParamsVO) {
        String token= shoppingCartParamsVO.getToken();
//        System.out.println(token);
        ShoppingCart shoppingCart= shoppingCartParamsVO;
        Integer flag=0;
        if (shoppingCart!=null){
            flag=shoppingCartMapper.insert(shoppingCart);
            if (flag!=0){
                return HttpResponse.success("添加成功");
            }else {
                return HttpResponse.error("添加失败");
            }
        }else {
            return HttpResponse.error("参数为控");
        }
    }

    @Override
    @Transactional
    public HttpResponse updateCartNumByCartId(ShoppingCartUpdateCartNumParamVO paramVO) {
        UpdateWrapper<ShoppingCart> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("cart_num",paramVO.getCartNum()).eq("cart_id",paramVO.getCartId());
        int updateFlag = shoppingCartMapper.update(null, updateWrapper);
        if (updateFlag!=0){
            return HttpResponse.success("修改成功");
        }else {
             return HttpResponse.error("修改失败");
        }
    }

    @Override
    public HttpResponse getShoppingCartsByCartIds(List<Integer> cartIds) {
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.getShoppingCartsByCartIds(cartIds);
        System.out.println(shoppingCarts);
        if (!CollectionUtils.isEmpty(shoppingCarts)){
            return HttpResponse.success(shoppingCarts);
        }else {
            return HttpResponse.error("根据cartIds查询购物车列表失败");
        }
    }

    @Override
    @Transactional
    public HttpResponse deleteShoppingCart(int parseInt) {
        UpdateWrapper<ShoppingCart> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("is_delete",1).eq("cart_id",parseInt);
        int flag = shoppingCartMapper.update(null, updateWrapper);
        if (flag!=0) {
            return HttpResponse.success("修改成功");
        }else {
            return HttpResponse.error("修改失败");
        }
    }
}
